package com.oauth.global.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HasInit;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.components.client.ui.footer.FooterUI;
import com.oauth.components.client.ui.header.HeaderUI;
import com.oauth.facebook.client.ui.FacebookInfo;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;
import com.oauth.global.client.ui.HomeWidgetInfo;
import com.oauth.global.client.ui.ManageInfo;
import com.oauth.global.client.ui.WidgetMenu;
import com.oauth.global.client.ui.WidgetMenuClickListener;
import com.oauth.global.client.ui.WidgetMenuItem;
import com.oauth.global.client.ui.WidgetPanel;
import com.oauth.global.client.ui.extension.ExtensionWidgetInfo;
import com.oauth.google.client.ui.docs.DocsInfo;
import com.oauth.google.client.ui.orkut.OrkutInfo;
import com.oauth.google.client.ui.picasa.PicasaInfo;
import com.oauth.google.client.ui.youtube.YoutubeInfo;
import com.oauth.linkedin.client.ui.LinkedinInfo;
import com.oauth.twitter.client.ui.TwitterInfo;

public class LocalEntryPoint implements HasWidgets,EntryPoint {

	private static StandardLayoutUiBinder uiBinder = GWT.create(StandardLayoutUiBinder.class);

	interface StandardLayoutUiBinder extends UiBinder<DockLayoutPanel, LocalEntryPoint> {
	}

	@UiField
	HeaderUI header;

	@UiField
	FooterUI footer;

	@UiField
	FlowPanel centerPanel;

	@UiField
	VerticalPanel westPanel;

	@UiField
	WidgetMenu widgetMenu;

	@UiField
	WidgetPanel widgetPanel;

	@UiField
	HasInit birthdayNotification;
	
	Map<Long, HasWidgetInfo> widgetMap = new HashMap<Long, HasWidgetInfo>();

	public void onModuleLoad() {
		DockLayoutPanel p = uiBinder.createAndBindUi(this);
		header.setUserName(DOM.getElementById("headerEmailId").getInnerText());
		widgetMap.put(1l, (HasWidgetInfo) GWT.create(TwitterInfo.class));
		widgetMap.put(2l, (HasWidgetInfo) GWT.create(OrkutInfo.class));
		widgetMap.put(3l, (HasWidgetInfo) GWT.create(LinkedinInfo.class));
		widgetMap.put(4l, (HasWidgetInfo) GWT.create(FacebookInfo.class));
		widgetMap.put(5l, (HasWidgetInfo) GWT.create(DocsInfo.class));
		widgetMap.put(6l, (HasWidgetInfo) GWT.create(PicasaInfo.class));
		widgetMap.put(7l, (HasWidgetInfo) GWT.create(YoutubeInfo.class));
		addManageMenu();
		addHomeMenus();
		addExtension();
		initMyApplications();
		RootPanel.get().add(p);
	}

	
	/**
	 * 
	 */
	private void addExtension() {
		HasWidgetInfo extensionWidget = (HasWidgetInfo)GWT.create(ExtensionWidgetInfo.class);
		widgetMap.put(extensionWidget.getId(),extensionWidget );
	}


	public void initMyApplications() {
		GlobalRemoteServiceAsync.INSTANCE
				.getMyApplications(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {
					public void onSuccess(List<ApplicationClientBean> result) {
						Long id = getShowParam();
						boolean found = false;
						if (result != null && result.size() > 0) {
							widgetMenu.addMenuSeparator();

							for (ApplicationClientBean application : result) {
								HasWidgetInfo w = widgetMap.get(application.getId());
								WidgetMenuItem menuItem = new WidgetMenuItem(application.getName(),
										w.getIcon());
								menuItem.addClickHandler(new WidgetMenuClickListener(w, widgetPanel));
								widgetMenu.addMenu(menuItem);
								if (w.getId() == id) {
									found = true;
									widgetPanel.setWidget(w);
								}
							}
						}
						if (found == false) {
							if(id == null){
								// Default set to home widgets.
								HasWidgetInfo hasWidgetInfo = widgetMap.get(HomeWidgetInfo.APPLICATION_ID);
								Window.alert(hasWidgetInfo.getName());
								widgetPanel.setWidget(hasWidgetInfo);
							}else{
								//Possible widgets are Manage & extension.
								HasWidgetInfo hasWidgetInfo = widgetMap.get(id);
								widgetPanel.setWidget(hasWidgetInfo);
							}
						}
					}

					private Long getShowParam() {
						String show = Window.Location.getParameter("show");
						return show != null ? Long.parseLong(show) : null;
					}
				});
			birthdayNotification.init();

	}

	/**
	 * Method to add widgets to center panel.
	 * 
	 * @param pW
	 */
	public void add(Widget pW) {
		widgetPanel.setWidget((HasWidgetInfo) pW);
	}

	@Override
	public void clear() {
		centerPanel.clear();

	}

	@Override
	public Iterator<Widget> iterator() {
		return centerPanel.iterator();
	}

	@Override
	public boolean remove(Widget pW) {
		return centerPanel.remove(pW);
	}

	private void addHomeMenus() {
		HomeWidgetInfo homeInfo = GWT.create(HomeWidgetInfo.class);
		widgetMap.put(homeInfo.getId(), homeInfo);

		homeInfo.setWidgetMap(widgetMap);

		WidgetMenuItem menuItem1 = new WidgetMenuItem(homeInfo);
		menuItem1.addClickHandler(new WidgetMenuClickListener(homeInfo, widgetPanel));
		widgetMenu.addMenu(menuItem1);
	}

	private void addManageMenu() {
		ManageInfo browse = GWT.create(ManageInfo.class);
		widgetMap.put(browse.getId(), browse);
		WidgetMenuItem menuItem1 = new WidgetMenuItem(browse);
		browse.setMaps(widgetMap);
		menuItem1.addClickHandler(new WidgetMenuClickListener(browse, widgetPanel));
		widgetMenu.addMenu(menuItem1);
		widgetMenu.addMenuSeparator();
	}

}
